// pages/address/index.js
Page({
  data: {
    addressList: [],
    loading: true,
    isEmpty: false
  },

  onLoad: function (options) {
    // 如果是从其他页面选择地址跳转过来的，设置选择模式
    if (options.select) {
      this.setData({
        isSelectMode: options.select === 'true'
      });
    }
    
    this.loadAddressList();
  },

  onShow: function () {
    // 页面显示时重新加载地址列表，以便在添加/编辑地址后刷新
    this.loadAddressList();
  },

  // 加载地址列表
  loadAddressList: function () {
    this.setData({ loading: true });
    
    // 模拟API请求，实际开发中应替换为真实的API调用
    setTimeout(() => {
      const addressList = this.getMockAddressList();
      
      this.setData({
        addressList: addressList,
        loading: false,
        isEmpty: addressList.length === 0
      });
    }, 1000);
  },

  // 添加新地址
  addAddress: function () {
    wx.navigateTo({
      url: '/pages/address-edit/index'
    });
  },

  // 编辑地址
  editAddress: function (event) {
    const addressId = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/address-edit/index?id=${addressId}`
    });
  },

  // 删除地址
  deleteAddress: function (event) {
    const addressId = event.currentTarget.dataset.id;
    
    wx.showModal({
      title: '提示',
      content: '确定要删除此地址吗？',
      success: (res) => {
        if (res.confirm) {
          // 模拟删除地址API调用
          wx.showLoading({ title: '处理中...' });
          
          setTimeout(() => {
            // 从地址列表中移除该地址
            const newList = this.data.addressList.filter(item => item.id !== addressId);
            
            this.setData({
              addressList: newList,
              isEmpty: newList.length === 0
            });
            
            wx.hideLoading();
            wx.showToast({
              title: '地址已删除',
              icon: 'success'
            });
          }, 1000);
        }
      }
    });
  },

  // 设置默认地址
  setDefaultAddress: function (event) {
    const addressId = event.currentTarget.dataset.id;
    
    // 模拟设置默认地址API调用
    wx.showLoading({ title: '处理中...' });
    
    setTimeout(() => {
      // 更新地址列表，将选中的地址设为默认
      const newList = this.data.addressList.map(item => {
        return {
          ...item,
          isDefault: item.id === addressId
        };
      });
      
      this.setData({
        addressList: newList
      });
      
      wx.hideLoading();
      wx.showToast({
        title: '已设为默认地址',
        icon: 'success'
      });
    }, 1000);
  },

  // 选择地址（在选择模式下）
  selectAddress: function (event) {
    if (!this.data.isSelectMode) return;
    
    const addressId = event.currentTarget.dataset.id;
    const selectedAddress = this.data.addressList.find(item => item.id === addressId);
    
    if (selectedAddress) {
      // 将选中的地址信息返回给上一页
      const pages = getCurrentPages();
      const prevPage = pages[pages.length - 2];
      
      // 调用上一个页面的方法，传递选中的地址
      if (prevPage && prevPage.setSelectedAddress) {
        prevPage.setSelectedAddress(selectedAddress);
      }
      
      wx.navigateBack();
    }
  },

  // 模拟获取地址列表数据
  getMockAddressList: function () {
    return [
      {
        id: '1',
        name: '张三',
        phone: '13800138000',
        province: '广东省',
        city: '深圳市',
        district: '南山区',
        address: '科技园南路XX号XX大厦X座XXX室',
        isDefault: true
      },
      {
        id: '2',
        name: '李四',
        phone: '13900139000',
        province: '广东省',
        city: '广州市',
        district: '天河区',
        address: '天河路XX号XX广场X座XXX室',
        isDefault: false
      },
      {
        id: '3',
        name: '王五',
        phone: '13700137000',
        province: '北京市',
        city: '北京市',
        district: '朝阳区',
        address: '朝阳路XX号XX大厦X座XXX室',
        isDefault: false
      }
    ];
  }
});